chgrp command
chgrp
- change group ownership
The chgrp
command in Linux is used to change the group ownership of files and directories. It’s a straightforward tool for managing which group has access to a resource, complementing commands like chown
(which also changes user ownership).
Note: Changing group ownership typically requires appropriate permissions—either ownership of the file or root privileges (using sudo
).
Usage: chgrp [OPTION]... GROUP FILE...
GROUP
: The new group name or group ID (GID) to assign.FILE
: The target file(s) or directory(s).OPTION
: Flags to modify behavior (e.g.,-R
for recursive).
Common Options
Option | Description |
---|---|
-R | Recursive (apply to directory contents) |
-v | Verbose output |
--reference | Copy group from another file |
-c | Report only when a change is made |
-f | Suppress error messages |
Examples
-
Basic Usage
Change the group of a file to a specified group.
chgrp developers file.txt
- Sets the group of
file.txt
todevelopers
. - Check with:
ls -l file.txt
- Output (example):
-rw-r--r-- 1 alice developers 0 Apr 1 10:00 file.txt
.
- Output (example):
- Sets the group of
-
Using Group ID (GID)
Specify a numeric GID instead of a group name.
chgrp 1001 file.txt
- Sets the group to GID 1001 (find GIDs in
/etc/group
or withgetent group developers
).
- Sets the group to GID 1001 (find GIDs in
-
Recursive Changes
Use
-R
to change the group of a directory and all its contents.chgrp -R developers /var/www
- Sets the group to
developers
for/var/www
and everything inside. - Verify:
ls -lR /var/www
- Sets the group to
-
Verbose Output
Use
-v
to see what changes are made.chgrp -v developers file.txt
- Output:
changed group of 'file.txt' to developers
.
- Output:
-
Copying Group Ownership
Use
--reference
to copy the group from another file.chgrp --reference=template.txt file.txt
- Sets
file.txt
’s group to matchtemplate.txt
’s group.
- Sets
-
Checking Groups
View available groups or confirm changes.
List Groups:
cat /etc/group
- Output (partial):
developers:x:1001:alice,bob
.
Check File:
ls -l file.txt
- Look at the group field (second column after permissions).
- Output (partial):
To get help related to the chgrp
command use --help
option
$ chgrp --help
Usage: chgrp [OPTION]... GROUP FILE...
or: chgrp [OPTION]... --reference=RFILE FILE...
Change the group of each FILE to GROUP.
With --reference, change the group of each FILE to that of RFILE.
-c, --changes like verbose but report only when a change is made
-f, --silent, --quiet suppress most error messages
-v, --verbose output a diagnostic for every file processed
--dereference affect the referent of each symbolic link (this is
the default), rather than the symbolic link itself
-h, --no-dereference affect symbolic links instead of any referenced file
(useful only on systems that can change the
ownership of a symlink)
--no-preserve-root do not treat '/' specially (the default)
--preserve-root fail to operate recursively on '/'
--reference=RFILE use RFILE's group rather than specifying a
GROUP value
-R, --recursive operate on files and directories recursively
The following options modify how a hierarchy is traversed when the -R
option is also specified. If more than one is specified, only the final
one takes effect.
-H if a command line argument is a symbolic link
to a directory, traverse it
-L traverse every symbolic link to a directory
encountered
-P do not traverse any symbolic links (default)
--help display this help and exit
--version output version information and exit
Examples:
chgrp staff /u Change the group of /u to "staff".
chgrp -hR staff /u Change the group of /u and subfiles to "staff".
For more details, check the manual with man chgrp